import 'package:awesome_project/utils/ext/build_context_ext.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mpflutter_core/mpflutter_core.dart';

import '../local/screens.dart';
import '../utils/show_utils.dart';
import 'gaps.dart';

class ImageWidget extends StatelessWidget {
  final String url;

  const ImageWidget({super.key, required this.url});

  @override
  Widget build(BuildContext context) {
    return Image(
      image: MPFlutterNetworkImage(url),
      fit: BoxFit.cover,
      loadingBuilder: (_, widget, loadingProgress) {
        if (loadingProgress == null) {
          return widget;
        }
        return Container(
          color: Colors.grey,
        );
      },
    );
  }
}

class RoundOutText extends StatelessWidget {
  final String title;
  final Color color;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          borderRadius: const BorderRadius.all(Radius.circular(10)),
          border: Border.all(width: 1, color: color)),
      padding: const EdgeInsets.symmetric(horizontal: 2, vertical: 1),
      child: Text(
        title,
        style: TextStyle(color: color, fontSize: 10),
      ),
    );
  }

  const RoundOutText({super.key, required this.title, required this.color});
}

class ArrowUpDown extends StatelessWidget {
  final int type;

  const ArrowUpDown({super.key, required this.type});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Transform.translate(
          offset: const Offset(0, 6),
          child: Icon(Icons.arrow_drop_up,
              color: type == 2 ? Colors.red : Colors.grey, size: 20),
        ),
        Transform.translate(
          offset: const Offset(0, -6),
          child: Icon(Icons.arrow_drop_down,
              color: type == 1 ? Colors.red : Colors.grey, size: 20),
        ),
      ],
    );
  }
}





class MyTextFiled extends StatelessWidget {
  final TextEditingController controller;
  final String placeholder;
  final TextAlign textAlign;

  const MyTextFiled(
      {super.key,
      required this.controller,
      required this.placeholder,
      this.textAlign = TextAlign.center});

  @override
  Widget build(BuildContext context) {
    const style =
        TextStyle(fontSize: 12, color: CupertinoColors.placeholderText);
    final decoration = BoxDecoration(
      color: Colors.grey.withOpacity(0.1),
      borderRadius: const BorderRadius.all(Radius.circular(5.0)),
    );

    ///onTapOutside
    return CupertinoTextField(
      controller: controller,
      textAlign: textAlign,
      onTapOutside: (event) {
        FocusManager.instance.primaryFocus?.unfocus();
      },
      decoration: decoration,
      placeholder: placeholder,
      placeholderStyle: style,
    );
  }
}

class LineThroughWidget extends StatelessWidget {
  final Widget child;

  const LineThroughWidget({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        child,
        const Positioned.fill(
            child: Align(
          alignment: Alignment(0, 0.2),
          child: Gap.h(double.infinity, color: Colors.grey, height: 1),
        ))
      ],
    );
  }
}

class PriceWidget extends StatelessWidget {
  final String? price;
  final bool isLineThrough;

  const PriceWidget(
      {super.key, this.isLineThrough = false, required this.price});

  @override
  Widget build(BuildContext context) {
    final priceStr = ShowUtils.getShowPrice(price);
    final child = Padding(
      padding: EdgeInsets.only(top: isLineThrough ? 3.0 : 0),
      child: Row(
        children: [
          Baseline(
            baseline: isLineThrough ? 1 : 6,
            baselineType: TextBaseline.alphabetic,
            child: Text(
              '¥',
              style: TextStyle(
                  fontSize: isLineThrough ? 8 : 10,
                  color: isLineThrough ? Colors.grey : Colors.red),
            ),
          ),
          Text(
            priceStr,
            style: TextStyle(
                fontSize: isLineThrough ? 10 : 16,
                color: isLineThrough ? Colors.grey : Colors.red,
                fontWeight: isLineThrough ? null : FontWeight.bold),
          ),
        ],
      ),
    );
    return isLineThrough ? LineThroughWidget(child: child) : child;
  }
}

class MessageNumWidget extends StatelessWidget {
  final String? num;
  final Widget child;

  const MessageNumWidget({super.key, this.num, required this.child});

  @override
  Widget build(BuildContext context) {
    final numInt = int.tryParse(num ?? "0") ?? 0;
    return Badge(
      backgroundColor: Colors.red,
      label: Text(numInt > 99 ? '99+' : '$numInt'),
      isLabelVisible: numInt > 0,
      offset: const Offset(10, -5),
      child: child,
    );
  }
}

class ArrowRightWidget extends StatelessWidget {
  final Color color;
  final double size;

  const ArrowRightWidget({super.key, this.color = Colors.grey, this.size = 16});

  @override
  Widget build(BuildContext context) {
    return Icon(
      Icons.arrow_forward_ios,
      color: color,
      size: size,
    );
  }
}

class MyListTitle extends StatelessWidget {
  final Widget? title, leading, trailing, subtitle;
  final GestureTapCallback? onTap;
  final Color? tileColor;
  final bool isContentPadding;

  const MyListTitle(
      {super.key,
      this.title,
      this.subtitle,
      this.leading,
      this.trailing,
      this.onTap,
      this.tileColor,
      this.isContentPadding = true});

  @override
  Widget build(BuildContext context) {
    return ListTile(
      contentPadding: isContentPadding ? null : EdgeInsets.zero,
      dense: true,
      tileColor: tileColor,
      visualDensity: VisualDensity.compact,
      title: title,
      subtitle: subtitle,
      leading: leading,
      trailing: trailing,
      onTap: onTap,
    );
  }
}

class NumberBigSmallText extends StatelessWidget {
  final String? price, pre;
  final Color color;

  const NumberBigSmallText(
      {super.key, this.price, required this.color, this.pre = ""});

  @override
  Widget build(BuildContext context) {
    final priceStr = ShowUtils.getShowPrices(price);
    return Row(
      children: [
        Baseline(
            baseline: 5,
            baselineType: TextBaseline.alphabetic,
            child: Text("$pre¥", style: TextStyle(color: color, fontSize: 12))),
        Text(
          priceStr.bigNum,
          style: TextStyle(color: color, fontSize: 20),
        ),
        Text(".", style: TextStyle(color: color)),
        Text(
          priceStr.smallNum,
          style: TextStyle(color: color, fontSize: 14),
        ),
      ],
    );
  }
}

class AddressTagWidget extends StatelessWidget {
  final String tag;
  final Color color;

  const AddressTagWidget({super.key, required this.tag, required this.color});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 3, vertical: 1),
      decoration: BoxDecoration(
          color: color.withOpacity(0.2),
          borderRadius: BorderRadius.circular(10)),
      child: Text(
        "$tag ",
        style: context.textTheme.labelSmall?.copyWith(color: color),
      ),
    );
  }
}

class CouponItemBg extends StatelessWidget {
  final bool isEnd;
  final Widget leftWidget, rightWidget;
  final Widget? icon;
  final Color? leftColor;

  const CouponItemBg(
      {super.key,
      required this.leftWidget,
      required this.rightWidget,
      this.icon,
      this.leftColor,
      required this.isEnd});

  @override
  Widget build(BuildContext context) {
    final color = isEnd ? Colors.grey : Colors.red;
    const borderWidth = 0.2;
    return Stack(
      children: [
        SizedBox(
          height: 100,
          child: Row(
            children: [
              Container(
                width: 100,
                height: 100,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.horizontal(
                        left: Radius.circular(10)),
                    border: Border(
                      left: BorderSide(color: color, width: borderWidth),
                      top: BorderSide(color: color, width: borderWidth),
                      bottom: BorderSide(color: color, width: borderWidth),
                    ),
                    color: leftColor),
                child: leftWidget,
              ),
              Expanded(
                  child: Container(
                height: 100,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    borderRadius: const BorderRadius.horizontal(
                        right: Radius.circular(10)),
                    border: Border(
                      right: BorderSide(color: color, width: borderWidth),
                      top: BorderSide(color: color, width: borderWidth),
                      bottom: BorderSide(color: color, width: borderWidth),
                    ),
                    color: Colors.white),
                child: rightWidget,
              ))
            ],
          ),
        ),
        Positioned(
          left: 90,
          top: -10,
          child: Container(
            width: 20,
            height: 20,
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
                border: Border.all(color: color, width: borderWidth)),
          ),
        ),
        Positioned(
          left: 90,
          bottom: -10,
          child: Container(
            width: 20,
            height: 20,
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
                border: Border.all(color: color, width: borderWidth)),
          ),
        ),
        if (icon != null)
          Positioned(
            top: 0,
            right: 0,
            child: icon!,
          )
      ],
    );
  }
}

class BottomButton extends StatelessWidget {
  final VoidCallback? onPressed;
  final Widget child;

  const BottomButton({super.key, this.onPressed, required this.child});

  @override
  Widget build(BuildContext context) {
    return FractionallySizedBox(
      child: TextButton(
        onPressed: onPressed,
        child: child,
      ),
    );
  }
}

class ScreenWidthBox extends StatelessWidget {
  final double height;
  final Widget child;

  const ScreenWidthBox({super.key, required this.height, required this.child});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 0,
      height: height,
      child: OverflowBox(
        minWidth: Screens.width,
        maxWidth: Screens.width,
        child: child,
      ),
    );
  }
}

class MyDivider extends StatelessWidget {
  const MyDivider({super.key});

  @override
  Widget build(BuildContext context) {
    return const Divider(thickness: 1, height: 1);
  }
}

class ClearWidget extends StatelessWidget {
  final VoidCallback? onPressed;

  const ClearWidget({super.key, this.onPressed});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 30,
      child: IconButton(
        icon: const Icon(Icons.clear),
        style: IconButton.styleFrom(padding: EdgeInsets.zero),
        onPressed: onPressed,
      ),
    );
  }
}
